package cn.phonevane.action;


import java.util.List;

import cn.phonevane.base.BaseAction;
import cn.phonevane.model.PageModel;
import cn.phonevane.model.TArticle;
import cn.phonevane.model.TTopic;
import cn.phonevane.service.ArticleService;
import cn.phonevane.service.TopicService;
import cn.phonevane.utils.PagingModule;
import cn.phonevane.utils.SystemConstant;

public class IndexAction extends BaseAction{
	private ArticleService  articleService;
	
	
	private TopicService topicService;

	public ArticleService getArticleService() {
		return articleService;
	}

	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}
	
	
	public TopicService getTopicService() {
		return topicService;
	}

	public void setTopicService(TopicService topicService) {
		this.topicService = topicService;
	}

	/**
	 * 获取文章数据
	 */
	public void getArticleList(){
		
		String str=httpServletRequest.getParameter("currentPage");
		int currentPage;
		if(str==null||str.equals("")){
			currentPage=1;
		}else{
			try{
				currentPage=Integer.parseInt(str.intern());
			}catch(NumberFormatException e){
				currentPage=1;
			}
		}
		List<TArticle> list=this.articleService.getPart(currentPage,SystemConstant.PAGE_SIZE);
		int count=this.articleService.getCount();
		int maxPage=PagingModule.maxPageSize(count);
		PageModel<TArticle> pm=new PageModel<TArticle>(list,maxPage);
		httpServletRequest.setAttribute("pm", pm);
		
	}
	
	/**
	 * 获取主题数据
	 */
	public void getTopicList(){
		List<TTopic> tList=this.topicService.getPart(SystemConstant.TOPIC_NUM);
		if(tList!=null&&tList.size()>0){
			httpServletRequest.setAttribute("TOPIC", tList);
		}
		
	}
	
	public String getIndex(){

		List<TArticle> aList=this.articleService.getPart(SystemConstant.NUM);
		getArticleList();
		getTopicList();
		if(aList!=null&&aList.size()>0){
			httpServletRequest.setAttribute("A_LIST", aList);
			return SUCCESS;
		}
		return ERROR;
	}
	

}
